package cn.hill4j.tool.spring.ext.feign.wrap;

import cn.hill4j.tool.core.wrap.WrapHandler;

import cn.hill4j.tool.core.wrap.WrapHandler.Invoker;

/**
 * ClassName: WrapFeignUtils
 * Description: TODO
 *
 * @author hillchen
 * Date: 2023年08月13日
 * @version: 1.0
 */
public class WrapFeignUtils {
    private static ThreadLocal<String> FEIGN_CLIENT_FLAG = new ThreadLocal<>();

    public static String getFeignClientFlag() {
        return FEIGN_CLIENT_FLAG.get();
    }

    public static void clearFeignClientFlag() {
        FEIGN_CLIENT_FLAG.remove();
    }

    public static void putFeignClientFlag(String clientFlag) {
        FEIGN_CLIENT_FLAG.set(clientFlag);
    }

    public static <T> T wrapInvokeFeign(Invoker<T> feignInvoker,String clientFlag){

        return WrapHandler.wrap(feignInvoker)
                .before(() -> putFeignClientFlag(clientFlag))
                .fin((r,e) -> clearFeignClientFlag())
                .invokeNoThrow();
    }
}
